package com.asia5b.a5mall.shop.activity

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5bmall.same.dialog.QCodeDilaog
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.config.A5BMallConfigCenter
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.event.CollectGoodsChange
import com.asia5b.a5mall.middleware.event.CollectShopChange
import com.asia5b.a5mall.middleware.event.LoginEvent
import com.asia5b.a5mall.middleware.logic.viewmodel.shop.ShopActivityVM
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.model.shop.ShopCollectModel
import com.asia5b.a5mall.middleware.model.shop.ShopGoodsInfoModel
import com.asia5b.a5mall.middleware.utils.getEventBus

import com.asia5b.a5mall.middleware.utils.joinComposite
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.databinding.ActivityShopdetailBinding
import kotlinx.android.synthetic.main.activity_shopdetail.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   店铺信息页面
 */
@Route(path = ActCons.ShopDetailActivity)
class ShopDetailActivity : SimpleActivity<ShopActivityVM, ActivityShopdetailBinding>() {
    override val viewModelClazz: Class<ShopActivityVM>
        get() = ShopActivityVM::class.java

    override fun getDataBinding(): ActivityShopdetailBinding {
        shopId = intent.getIntExtra("shopId", 0)
        return ActivityShopdetailBinding.inflate(layoutInflater)
    }

    override var sIsDark = true

    var qCodeDilaog: QCodeDilaog? = null
    var shopModel: AbsShopModel? = null
    var shopCollectModel: ShopCollectModel? = null

    var shopId: Int = 0

    override fun initView() {
        mViewModel.getShopDetail(shopId)
    }

    fun setCollectionView() {
        val isCollection = shopCollectModel?.message ?: false
        iv_shopdetail_collecticon.isSelected = isCollection
        iv_shopdetail_collecticon.text = (isCollection).judge(getString(R.string.已关注), ResString.addCollect())
    }

    fun setRatingView() {
        //店铺评分 rb_commentdetail_shop_star iv_commentdetail_starvalue
        //描述相符 rb_shop_descstar tv_shop_descstar
        //物流相符 rb_shop_expressstar tv_shop_expressstar
        //服务相符 rb_shop_serverstar tv_shop_serverstar

        shopModel?.run {
            //店铺评分
            rb_commentdetail_shop_star.rating = absShopCredit.toFloat()
            //店铺评分值
            iv_commentdetail_starvalue.text = String.format("%.1f", absShopCredit)
            //描述相符
            rb_shop_descstar.rating = absShopDescCredit.toFloat()
            tv_shop_descstar.text = String.format("%.1f", absShopDescCredit.toFloat())
            //物流相符
            rb_shop_expressstar.rating = absShopDeliveryCredit.toFloat()
            tv_shop_expressstar.text = String.format("%.1f", absShopDeliveryCredit.toFloat())
            //服务相符
            rb_shop_serverstar.rating = absShopServiceCredit.toFloat()
            tv_shop_serverstar.text = String.format("%.1f", absShopServiceCredit.toFloat())

            //服务电话
            //所在地址
            //身份验证
            //店铺认证
        }

    }

    override fun initEvent() {
        iv_shopdetail_qcode.setOnClickListener {
            shopModel?.let {
                if (qCodeDilaog == null) {
                    qCodeDilaog = QCodeDilaog(it, mActivity)
                }
                qCodeDilaog?.show()
            }

        }
        iv_shopdetail_collecticon.setOnClickListener {
            if (UserRepository.isLogin(mActivity)) {
                mViewModel.collectShop(shopModel?.absShopId ?: 0, shopCollectModel?.message
                        ?: false)
            }
        }
        head_shopinfo.setRightClickListener {
            mViewModel.share(mActivity,
                    A5BMallConfigCenter.INSTANCE.WAP_SELLER_URL + "$shopId",
                    shopModel!!.absShopLogo
                            ?: "", getString(R.string.武时亿商城), shopModel!!.absShopName)
        }
    }

    override fun startObserver() {
        mViewModel.getShopDetailResultLiveData.observe(this, Observer {
            getShopDetailResult(it)
        })
        mViewModel.isCollectShopResultLiveData.observe(this, Observer {
            isCollectShopResult(it)
        })
        mViewModel.collectShopResultLiveData.observe(this, Observer {
            collectShopResult(it)
        })
    }

    override fun initData() {

    }

    fun getShopDetailResult(data: Array<Any>) {
        shopModel = data[0] as AbsShopModel
        shopCollectModel = data[1] as ShopCollectModel
        mDataBinding.shopModel = shopModel
        mDataBinding.shopGoodsInfoModel = data[2] as ShopGoodsInfoModel
        setCollectionView()
        setRatingView()
    }

    fun isCollectShopResult(data: ShopCollectModel) {
        shopCollectModel = data
        setCollectionView()
    }

    fun collectShopResult(collect: Boolean) {
        shopCollectModel?.message = !collect
        setCollectionView()
        getEventBus().post(CollectGoodsChange())
        getEventBus().post(CollectShopChange(!collect, shopId))
    }

    override val isRegisterEventBus: Boolean
        get() = true

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoginEvent(event: LoginEvent) {
        mViewModel.isCollectShop(shopId)
    }
}